package charpter01_basic.lession05_impl_producter_consumer.my_queue;

import java.util.LinkedList;

public class MyBlockingQueue {
    private final LinkedList<Integer> queue;
    private final int max;

    public MyBlockingQueue(int size){
        this.max = size;
        this.queue = new LinkedList<>();
    }

    public synchronized void put(Integer i){
        while (queue.size() == max){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        queue.add(i);
        notifyAll();
    }

    public synchronized Integer take(){
        while (queue.size() == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Integer remove = queue.remove();
        notifyAll();
        return remove;
    }

    public int size() {
        return queue.size();
    }
}
